package im.vinci.server.music.service;

import im.vinci.server.common.exceptions.VinciException;
import im.vinci.server.common.exceptions.error.ErrorCode;
import im.vinci.server.music.domain.Artist;
import im.vinci.server.music.domain.MusicStyle;
import im.vinci.server.music.domain.MusicalPreference;
import im.vinci.server.music.persistence.MusicStyleFileMapper;
import im.vinci.server.music.persistence.MusicalPreferenceMapper;
import im.vinci.server.utils.BizTemplate;
import im.vinci.server.utils.JsonUtils;
import im.vinci.server.utils.UserContext;
import org.elasticsearch.common.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mingjie on 17/3/11.
 */
@Service
public class MusicalPreferenceService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MusicalPreferenceMapper musicalPreferenceMapper;

    @Autowired
    private MusicStyleFileMapper musicStyleFileMapper;

    public List<MusicStyle> getMusicStyleData() {
        return new BizTemplate<List<MusicStyle>>(getClass().getSimpleName() + "getMusicStyleData") {
            @Override
            protected void checkParams() throws VinciException {

            }

            @Override
            protected List<MusicStyle> process() throws Exception {
                return musicStyleFileMapper.getData();
            }
        }.execute();
    }

    public Boolean postMusicPreference(List<MusicStyle> dataList) {
        return new BizTemplate<Boolean>(getClass().getSimpleName() + "postMusicPreference") {
            @Override
            protected void checkParams() throws VinciException {
                if (dataList == null || dataList.isEmpty()) {
                    throw new VinciException(ErrorCode.ARGUMENT_ERROR, "数据格式错误", "");
                }

                boolean isValid = true;
                for (MusicStyle musicStyle : dataList) {
                    if (musicStyle == null || Strings.isNullOrEmpty(musicStyle.getName()) || musicStyle.getArtists() == null) {
                        isValid = false;
                    } else {
                        for (Artist artist : musicStyle.getArtists()) {
                            if (artist.getArtistId() <= 0 || Strings.isNullOrEmpty(artist.getName())) {
                                isValid = false;
                                break;
                            }

                            artist.setImage(null);
                            artist.setStyle(null);
                        }
                    }

                    if (!isValid) {
                        throw new VinciException(ErrorCode.ARGUMENT_ERROR, "参数错误, 数据为空或artist_id小于0", "");
                    }
                }
            }

            @Override
            protected Boolean process() throws Exception {
                List<MusicalPreference> list = new ArrayList<MusicalPreference>();
                for (MusicStyle musicStyle : dataList) {

                    MusicalPreference musicPreference = new MusicalPreference();
                    musicPreference.setUserId(UserContext.getUserInfo().getId());
                    musicPreference.setMusicStyle(musicStyle.getName());
                    musicPreference.setArtists(JsonUtils.encode(musicStyle.getArtists()));
                    list.add(musicPreference);
                }

                if (!list.isEmpty()) {
                    musicalPreferenceMapper.delete(UserContext.getUserInfo().getId());
                    musicalPreferenceMapper.insert(list);
                }
                return true;
            }
        }.execute();
    }

    public List<MusicStyle> getMusicPreference(long userId) {
        return new BizTemplate<List<MusicStyle>>(getClass().getSimpleName() + "getMusicPreference") {
            @Override
            protected void checkParams() throws VinciException {

            }

            @Override
            protected List<MusicStyle> process() throws Exception {
                List<MusicalPreference> list = musicalPreferenceMapper.select(userId);

                List<MusicStyle> resultList = new ArrayList<MusicStyle>();
                for (MusicalPreference musicalPreference : list) {
                    MusicStyle musicStyle = new MusicStyle();
                    musicStyle.setName(musicalPreference.getMusicStyle());

                    List<Artist> artistList = JsonUtils.decode(musicalPreference.getArtists(), List.class);
                    musicStyle.setArtists(artistList);

                    resultList.add(musicStyle);
                }

                return resultList;
            }
        }.execute();
    }
}
